import { query, mutation } from '@/utils/http'

// 获取微信用户信息
function apiGetWechatInfo() {
  return new Promise((resolve, reject) => {
    wx.getUserInfo({
      success: res => {
        const { encryptedData, iv } = res
        if (encryptedData && iv) {
          resolve({ encryptedData, iv })
        }
      },
      fail: err => reject(err)
    })
  })
}

// 把微信用户信息保存到账户
function apiSaveWecahrtUserInfo(token, info) {
  const str = `
    mutation ($info:AuthWechatUserRequest){
      authWechatUser(request:$info)
    }
  `
  return new Promise((resolve, reject) => {
    mutation(token, str, { info })
      .then(res => {
        if (res.data.data && res.data.data.authWechatUser) {
          resolve('success')
        } else {
          reject('fail')
        }
      })
      .catch(err => reject(err))
  })
}

// 获取用户(微信信息+账户信息)
function apiGetUserInfo(token) {
  const str = `
    {
      wechatUserInfo{
        name
        mobile
        email
        identityType
        identityNumber
        gender
        nickname
        headImgUrl
        country
        province
        city
        countryCode
        language
      }
    }
  `
  return new Promise((resolve, reject) => {
    query(token, str)
      .then(res => {
        if (res.data.data && res.data.data.wechatUserInfo) {
          resolve(res.data.data.wechatUserInfo)
        } else {
          reject()
        }
      })
      .catch(err => reject(err))
  })
}

// 检查用户是否已完善信息
function apiCheckInfo(token) {
  const str = `
    {
      needImprove
    }
  `
  return new Promise((resolve, reject) => {
    query(token, str)
      .then(res => {
        res.data.data ? resolve(!res.data.data.needImprove) : reject(new Error('fail'))
      })
      .catch(err => reject(err))
  })
}

// 修改用户信息
function apiImproveInfo(token, info) {
  const str = `
    ($request:ImproveUserInfoRequest!){
      improveUserInfo(request:$request)
    }
  `
  return new Promise((resolve, reject) => {
    mutation(token, str, { request: info })
      .then(res => {
        res.data.data.improveUserInfo ? resolve() : reject(new Error('fail'))
      })
      .catch(err => reject(err))
  })
}

function apiGetTravelers(token) {
  const str = `
    {
      userTravelers{
        id
        name
        countryCode
        mobile
        identityType
        identityNumber
        status
      }
    }
  `
  return new Promise((resolve, reject) => {
    query(token, str)
      .then(res => {
        if (res.data.data && res.data.data.userTravelers) {
          resolve(res.data.data.userTravelers)
        } else {
          reject(new Error('fail'))
        }
      })
      .catch(err => reject(err))
  })
}

function apiAddTraveler(token, data) {
  const str = `
    ($input:UserTravelerInput!){
    addUserTraveler(input: $input){
      id
    }
  }`
  return new Promise((resolve, reject) => {
    mutation(token, str, { input: data })
      .then(res => {
        console.log(res)
        res.data.data && res.data.data.addUserTraveler ? resolve() : reject(new Error('fail'))
      })
      .catch(err => reject(err))
  })
}

function apiUpdateTraveler(token, data) {
  const str = `
  ($input:UserTravelerInput!){
    updateUserTraveler(input:$input)
  }`
  return new Promise((resolve, reject) => {
    mutation(token, str, { input: data })
      .then(res => {
        res.data.data && res.data.data.updateUserTraveler ? resolve() : reject(new Error('fail'))
      })
      .catch(err => reject(err))
  })
}

function apiDeleteTraveler(token, id) {
  const str = `
  ($id:Long!){
    deleteUserTraveler(id:$id)
  }`
  return new Promise((resolve, reject) => {
    mutation(token, str, { id })
      .then(res => {
        res.data.data && res.data.data.deleteUserTraveler ? resolve() : reject(new Error('fail'))
      })
      .catch(err => reject(err))
  })
}

export {
  apiGetWechatInfo,
  apiSaveWecahrtUserInfo,
  apiGetUserInfo,
  apiCheckInfo,
  apiImproveInfo,
  apiGetTravelers,
  apiAddTraveler,
  apiUpdateTraveler,
  apiDeleteTraveler
}
